/**
 * Copyright (c) 2021 qar All rights reserved.
 *
 * https://www.qar.io
 *
 * 版权所有，侵权必究！
 */

package com.example.controller;
import com.example.dto.SysPostDTO;
import com.example.dto.SysQueryPostDTO;
import com.example.model.api.Result;
import com.example.model.page.PageData;
import com.example.service.SysPostService;
import com.example.utils.ExcelUtils;
import com.example.validator.AssertUtils;
import com.example.validator.ValidatorUtils;
import com.example.validator.group.AddGroup;
import com.example.validator.group.DefaultGroup;
import com.example.validator.group.UpdateGroup;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
* 岗位管理
*
* @author Mark sunlightcs@gmail.com
*/
@RestController
@RequestMapping("post")
@Tag(name = "岗位管理")
public class SysPostController {
    @Autowired
    private SysPostService sysPostService;
//    @PostMapping("page")
//    @Operation(summary = "分页")
//    @PreAuthorize("hasAuthority('sys:user:page')")
////    @PreAuthorize("hasAuthority('message.read')")
//    public Result<PageData<SysUserDTO>> page(@RequestBody SysQueryUserDTO sysQueryUserDTO){
//        PageData<SysUserDTO> page = sysPostService.selectPage(sysQueryUserDTO);
//        return new Result<PageData<SysUserDTO>>().ok(page);
//    }



    @RequestMapping(value = "page", method = {RequestMethod.POST, RequestMethod.GET})
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('sys:post:page')")
    public Result<PageData<SysPostDTO>> page(@RequestBody SysQueryPostDTO sysQueryPostDTO){
        PageData<SysPostDTO> page = sysPostService.selectPage(sysQueryPostDTO);

        return new Result<PageData<SysPostDTO>>().ok(page);
    }

    @GetMapping("list")
    @Operation(summary = "列表")
    public Result<List<SysPostDTO>> list(){
        Map<String, Object> params = new HashMap<>();
        //正常岗位列表
        params.put("status", "1");
        List<SysPostDTO> data = sysPostService.list(params);

        return new Result<List<SysPostDTO>>().ok(data);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('sys:post:info')")
    public Result<SysPostDTO> get(@PathVariable("id") Long id){
        SysPostDTO data = sysPostService.get(id);

        return new Result<SysPostDTO>().ok(data);
    }

    @PostMapping
    @Operation(summary = "保存")
//    @LogOperation("保存")
    @PreAuthorize("hasAuthority('sys:post:save')")
    public Result<String> save(@RequestBody SysPostDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        sysPostService.save(dto);

        return new Result<String>().ok("ok");
    }

    @PutMapping
    @Operation(summary = "修改")
//    @LogOperation("修改")
    @PreAuthorize("hasAuthority('sys:post:update')")
    public Result<String> update(@RequestBody SysPostDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        sysPostService.update(dto);

        return new Result<String>().ok("ok");
    }

    @DeleteMapping
    @Operation(summary = "删除")
//    @LogOperation("删除")
    @PreAuthorize("hasAuthority('sys:post:delete')")
    public Result<String> delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        sysPostService.delete(ids);

        return new Result<String>().ok("ok");
    }
}